home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / sswitchxp152.exe / source / SpeedswitchXP / HyperLink.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-14  |  13.3 KB  |  441 lines

  1. // HyperLink.cpp : implementation file
  2. //
  3. // HyperLink static control. Will open the default browser with the given URL
  4. // when the user clicks on the link.
  5. //
  6. // Copyright (C) 1997, 1998 Chris Maunder (chrismaunder@codeguru.com)
  7. // All rights reserved. May not be sold for profit.
  8. //
  9. // Thanks to Pσl K. T°nder for auto-size and window caption changes.
  10. //
  11. // "GotoURL" function by Stuart Patterson
  12. // As seen in the August, 1997 Windows Developer's Journal.
  13. // Copyright 1997 by Miller Freeman, Inc. All rights reserved.
  14. // Modified by Chris Maunder to use TCHARs instead of chars.
  15. //
  16. // "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
  17. //
  18.  
  19. #define _UNICODE
  20.  
  21. #include "stdafx.h"
  22. #include "HyperLink.h"
  23.  
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. #define TOOLTIP_ID 1
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CHyperLink
  34.  
  35. CHyperLink::CHyperLink()
  36. {
  37.     m_hLinkCursor       = NULL;                 // No cursor as yet
  38.     m_crLinkColour      = RGB(  0,   0, 238);   // Blue
  39.     m_crVisitedColour   = RGB( 85,  26, 139);   // Purple
  40.     m_crHoverColour     = ::GetSysColor(COLOR_HIGHLIGHT);
  41.     m_bOverControl      = FALSE;                // Cursor not yet over control
  42.     m_bVisited          = FALSE;                // Hasn't been visited yet.
  43.     m_bUnderline        = TRUE;                 // Underline the link?
  44.     m_bAdjustToFit      = TRUE;                 // Resize the window to fit the text?
  45.     m_strURL.Empty();
  46. }
  47.  
  48. CHyperLink::~CHyperLink()
  49. {
  50.     m_Font.DeleteObject();
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
  54.     //{{AFX_MSG_MAP(CHyperLink)
  55.     ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
  56.     ON_WM_CTLCOLOR_REFLECT()
  57.     ON_WM_SETCURSOR()
  58.     ON_WM_MOUSEMOVE()
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CHyperLink message handlers
  64.  
  65. BOOL CHyperLink::PreTranslateMessage(MSG* pMsg) 
  66. {
  67.     m_ToolTip.RelayEvent(pMsg);
  68.     return CStatic::PreTranslateMessage(pMsg);
  69. }
  70.  
  71. void CHyperLink::OnClicked()
  72. {
  73.     BOOL result = GotoURL( m_strURL, SW_SHOW );
  74.     m_bVisited = result;
  75.     if (!m_bVisited) {
  76.         MessageBeep(MB_ICONEXCLAMATION);     // Unable to follow link
  77.         ReportError(result);
  78.     } else 
  79.         SetVisited();                        // Repaint to show visited colour
  80. }
  81.  
  82. HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor) 
  83. {
  84.     ASSERT(nCtlColor == CTLCOLOR_STATIC);
  85.  
  86.     if (m_bOverControl)
  87.         pDC->SetTextColor(m_crHoverColour);
  88.     else if (m_bVisited)
  89.         pDC->SetTextColor(m_crVisitedColour);
  90.     else
  91.         pDC->SetTextColor(m_crLinkColour);
  92.  
  93.     // transparent text.
  94.     pDC->SetBkMode(TRANSPARENT);
  95.     return (HBRUSH)GetStockObject(NULL_BRUSH);
  96. }
  97.  
  98. void CHyperLink::OnMouseMove(UINT nFlags, CPoint point) 
  99. {
  100.     CStatic::OnMouseMove(nFlags, point);
  101.  
  102.     if (m_bOverControl)        // Cursor is currently over control
  103.     {
  104.         CRect rect;
  105.         GetClientRect(rect);
  106.  
  107.         if (!rect.PtInRect(point))
  108.         {
  109.             m_bOverControl = FALSE;
  110.             ReleaseCapture();
  111.             RedrawWindow();
  112.             return;
  113.         }
  114.     }
  115.     else                      // Cursor has just moved over control
  116.     {
  117.         m_bOverControl = TRUE;
  118.         RedrawWindow();
  119.         SetCapture();
  120.     }
  121. }
  122.  
  123. BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/) 
  124. {
  125.     if (m_hLinkCursor)
  126.     {
  127.         ::SetCursor(m_hLinkCursor);
  128.         return TRUE;
  129.     }
  130.     return FALSE;
  131. }
  132.  
  133. void CHyperLink::PreSubclassWindow() 
  134. {
  135.     // We want to get mouse clicks via STN_CLICKED
  136.     DWORD dwStyle = GetStyle();
  137.     ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
  138.     
  139.     // Set the URL as the window text
  140.     if (m_strURL.IsEmpty())
  141.         GetWindowText(m_strURL);
  142.  
  143.     // Check that the window text isn't empty. If it is, set it as the URL.
  144.     CString strWndText;
  145.     GetWindowText(strWndText);
  146.     if (strWndText.IsEmpty()) {
  147.         ASSERT(!m_strURL.IsEmpty());    // Window and URL both NULL. DUH!
  148.         SetWindowText(m_strURL);
  149.     }
  150.  
  151.     // Create the font
  152.     LOGFONT lf;
  153.     GetFont()->GetLogFont(&lf);
  154.     lf.lfUnderline = m_bUnderline;
  155.     m_Font.CreateFontIndirect(&lf);
  156.     SetFont(&m_Font);
  157.  
  158.     PositionWindow();        // Adjust size of window to fit URL if necessary
  159.     SetDefaultCursor();      // Try and load up a "hand" cursor
  160.  
  161.     // Create the tooltip
  162.     CRect rect; 
  163.     GetClientRect(rect);
  164.     m_ToolTip.Create(this);
  165.     m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
  166.  
  167.     CStatic::PreSubclassWindow();
  168. }
  169.  
  170. /////////////////////////////////////////////////////////////////////////////
  171. // CHyperLink operations
  172.  
  173. void CHyperLink::SetURL(CString strURL)
  174. {
  175.     m_strURL = strURL;
  176.  
  177.     if (::IsWindow(GetSafeHwnd())) {
  178.         PositionWindow();
  179.         m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
  180.     }
  181. }
  182.  
  183. CString CHyperLink::GetURL() const
  184.     return m_strURL;   
  185. }
  186.  
  187. void CHyperLink::SetColours(COLORREF crLinkColour, COLORREF crVisitedColour,
  188.                             COLORREF crHoverColour /* = -1 */) 
  189.     m_crLinkColour    = crLinkColour; 
  190.     m_crVisitedColour = crVisitedColour;
  191.  
  192.     if (crHoverColour == -1)
  193.         m_crHoverColour = ::GetSysColor(COLOR_HIGHLIGHT);
  194.     else
  195.         m_crHoverColour = crHoverColour;
  196.  
  197.     if (::IsWindow(m_hWnd))
  198.         Invalidate(); 
  199. }
  200.  
  201. COLORREF CHyperLink::GetLinkColour() const
  202.     return m_crLinkColour; 
  203. }
  204.  
  205. COLORREF CHyperLink::GetVisitedColour() const
  206. {
  207.     return m_crVisitedColour; 
  208. }
  209.  
  210. COLORREF CHyperLink::GetHoverColour() const
  211. {
  212.     return m_crHoverColour;
  213. }
  214.  
  215. void CHyperLink::SetVisited(BOOL bVisited /* = TRUE */) 
  216.     m_bVisited = bVisited; 
  217.  
  218.     if (::IsWindow(GetSafeHwnd()))
  219.         Invalidate(); 
  220. }
  221.  
  222. BOOL CHyperLink::GetVisited() const
  223.     return m_bVisited; 
  224. }
  225.  
  226. void CHyperLink::SetLinkCursor(HCURSOR hCursor)
  227.     m_hLinkCursor = hCursor;
  228.     if (m_hLinkCursor == NULL)
  229.         SetDefaultCursor();
  230. }
  231.  
  232. HCURSOR CHyperLink::GetLinkCursor() const
  233. {
  234.     return m_hLinkCursor;
  235. }
  236.  
  237. void CHyperLink::SetUnderline(BOOL bUnderline /* = TRUE */)
  238. {
  239.     m_bUnderline = bUnderline;
  240.  
  241.     if (::IsWindow(GetSafeHwnd()))
  242.     {
  243.         LOGFONT lf;
  244.         GetFont()->GetLogFont(&lf);
  245.         lf.lfUnderline = m_bUnderline;
  246.  
  247.         m_Font.DeleteObject();
  248.         m_Font.CreateFontIndirect(&lf);
  249.         SetFont(&m_Font);
  250.  
  251.         Invalidate(); 
  252.     }
  253. }
  254.  
  255. BOOL CHyperLink::GetUnderline() const
  256.     return m_bUnderline; 
  257. }
  258.  
  259. void CHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)
  260. {
  261.     m_bAdjustToFit = bAutoSize;
  262.  
  263.     if (::IsWindow(GetSafeHwnd()))
  264.         PositionWindow();
  265. }
  266.  
  267. BOOL CHyperLink::GetAutoSize() const
  268.     return m_bAdjustToFit; 
  269. }
  270.  
  271.  
  272. // Move and resize the window so that the window is the same size
  273. // as the hyperlink text. This stops the hyperlink cursor being active
  274. // when it is not directly over the text. If the text is left justified
  275. // then the window is merely shrunk, but if it is centred or right
  276. // justified then the window will have to be moved as well.
  277. //
  278. // Suggested by Pσl K. T°nder 
  279.  
  280. void CHyperLink::PositionWindow()
  281. {
  282.     if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit) 
  283.         return;
  284.  
  285.     // Get the current window position
  286.     CRect rect;
  287.     GetWindowRect(rect);
  288.  
  289.     CWnd* pParent = GetParent();
  290.     if (pParent)
  291.         pParent->ScreenToClient(rect);
  292.  
  293.     // Get the size of the window text
  294.     CString strWndText;
  295.     GetWindowText(strWndText);
  296.  
  297.     CDC* pDC = GetDC();
  298.     CFont* pOldFont = pDC->SelectObject(&m_Font);
  299.     CSize Extent = pDC->GetTextExtent(strWndText);
  300.     pDC->SelectObject(pOldFont);
  301.     ReleaseDC(pDC);
  302.  
  303.     // Get the text justification via the window style
  304.     DWORD dwStyle = GetStyle();
  305.  
  306.     // Recalc the window size and position based on the text justification
  307.     if (dwStyle & SS_CENTERIMAGE)
  308.         rect.DeflateRect(0, (rect.Height() - Extent.cy)/2);
  309.     else
  310.         rect.bottom = rect.top + Extent.cy;
  311.  
  312.     if (dwStyle & SS_CENTER)   
  313.         rect.DeflateRect((rect.Width() - Extent.cx)/2, 0);
  314.     else if (dwStyle & SS_RIGHT) 
  315.         rect.left  = rect.right - Extent.cx;
  316.     else // SS_LEFT = 0, so we can't test for it explicitly 
  317.         rect.right = rect.left + Extent.cx;
  318.  
  319.     // Move the window
  320.     SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
  321. }
  322.  
  323. /////////////////////////////////////////////////////////////////////////////
  324. // CHyperLink implementation
  325.  
  326. // The following appeared in Paul DiLascia's Jan 1998 MSJ articles.
  327. // It loads a "hand" cursor from the winhlp32.exe module
  328. void CHyperLink::SetDefaultCursor()
  329. {
  330.     if (m_hLinkCursor == NULL)                // No cursor handle - load our own
  331.     {
  332.         // Get the windows directory
  333.         CString strWndDir;
  334.         GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
  335.         strWndDir.ReleaseBuffer();
  336.  
  337.         strWndDir += _T("\\winhlp32.exe");
  338.         // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
  339.         HMODULE hModule = LoadLibrary(strWndDir);
  340.         if (hModule) {
  341.             HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
  342.             if (hHandCursor)
  343.                 m_hLinkCursor = CopyCursor(hHandCursor);
  344.         }
  345.         FreeLibrary(hModule);
  346.     }
  347. }
  348.  
  349. LONG CHyperLink::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
  350. {
  351.     HKEY hkey;
  352.     LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
  353.  
  354.     if (retval == ERROR_SUCCESS) {
  355.         long datasize = MAX_PATH;
  356.         TCHAR data[MAX_PATH];
  357.         RegQueryValue(hkey, NULL, data, &datasize);
  358.         lstrcpy(retdata,data);
  359.         RegCloseKey(hkey);
  360.     }
  361.  
  362.     return retval;
  363. }
  364.  
  365. void CHyperLink::ReportError(int nError)
  366. {
  367.     CString str;
  368.     switch (nError) {
  369.         case 0:                       str = _T("The operating system is out\nof memory or resources."); break;
  370.         case SE_ERR_PNF:              str = _T("The specified path was not found."); break;
  371.         case SE_ERR_FNF:              str = _T("The specified file was not found."); break;
  372.         case ERROR_BAD_FORMAT:        str = _T("The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."); break;
  373.         case SE_ERR_ACCESSDENIED:     str = _T("The operating system denied\naccess to the specified file."); break;
  374.         case SE_ERR_ASSOCINCOMPLETE:  str = _T("The filename association is\nincomplete or invalid."); break;
  375.         case SE_ERR_DDEBUSY:          str = _T("The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."); break;
  376.         case SE_ERR_DDEFAIL:          str = _T("The DDE transaction failed."); break;
  377.         case SE_ERR_DDETIMEOUT:       str = _T("The DDE transaction could not\nbe completed because the request timed out."); break;
  378.         case SE_ERR_DLLNOTFOUND:      str = _T("The specified dynamic-link library was not found."); break;
  379.         case SE_ERR_NOASSOC:          str = _T("There is no application associated\nwith the given filename extension."); break;
  380.         case SE_ERR_OOM:              str = _T("There was not enough memory to complete the operation."); break;
  381.         case SE_ERR_SHARE:            str = _T("A sharing violation occurred. ");
  382.         default:                      str.Format(_T("Unknown Error (%d) occurred."), nError); break;
  383.     }
  384.     str = _T("Unable to open hyperlink:\n\n") + str;
  385.     AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
  386. }
  387.  
  388. BOOL CHyperLink::GotoURL(LPCTSTR url, int showcmd)
  389. {
  390.     TCHAR key[MAX_PATH + MAX_PATH];
  391.  
  392.     // First try ShellExecute()
  393.     BOOL result = (((UINT)ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd)) > HINSTANCE_ERROR);
  394.  
  395.     // If it failed, get the .htm regkey and lookup the program
  396.     if( !result )
  397.     {
  398.         if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
  399.             lstrcat(key, _T("\\shell\\open\\command"));
  400.  
  401.             if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
  402.                 TCHAR *pos;
  403.                 pos = _tcsstr(key, _T("\"%1\""));
  404.                 if (pos == NULL) {                     // No quotes found
  405.                     pos = _tcsstr(key, _T("%1"));       // Check for %1, without quotes 
  406.                     if (pos == NULL)                   // No parameter at all...
  407.                         pos = key+lstrlen(key)-1;
  408.                     else
  409.                         *pos = '\0';                   // Remove the parameter
  410.                 }
  411.                 else
  412.                     *pos = '\0';                       // Remove the parameter
  413.  
  414.                 lstrcat(pos, _T(" "));
  415.                 lstrcat(pos, url);
  416.                 result = CreateProcess( NULL,
  417.                                         key,
  418.                                         NULL,
  419.                                         NULL,
  420.                                         FALSE,
  421.                                         NORMAL_PRIORITY_CLASS,
  422.                                         NULL,
  423.                                         NULL,
  424.                                         NULL,
  425.                                         NULL );
  426.                 //result = (HINSTANCE) WinExec( key, showcmd );
  427.             }
  428.         }
  429.     }
  430.  
  431.     return result;
  432. }
  433.